package com.blacktry.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.blacktry.entity.easypoidata.DemoDataListener;
import com.blacktry.entity.easypoidata.DeriveExcel;
import com.blacktry.entity.easypoidata.CustomCellWriteHandler;
import com.blacktry.service.DeriveExcelService;
import com.blacktry.service.EasyPoiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
public class EasyPoiServiceImpl implements EasyPoiService {

    @Autowired
    private DeriveExcelService deriveExcelService;


    List<DeriveExcel> writeDataList = new ArrayList<>();
    private String path = "C:\\Users\\asus\\Desktop\\";
    private String writeName = "模板1";


    @Override
    public String write(DeriveExcel deriveExcel) throws MalformedURLException {
        String fileName = path + writeName + ".xls";
        writeDataList.add(deriveExcel);
        deriveExcel.setPaperImg(new URL(deriveExcel.getPaper()));

//        Double  reseve3=deriveExcel.getScanAnswer();
//        int b=reseve3.intValue();
//        Integer rentCount=Integer.valueOf(b);
//        deriveExcel.getScanAnswer()

        EasyExcel.write(fileName, DeriveExcel.class).sheet(deriveExcel.getScanAnswer()).registerWriteHandler(new CustomCellWriteHandler()).doWrite(writeDataList);
        return path+writeName;
//        return "已自动批阅";
    }

    @Override
    public void writeList(List<DeriveExcel> deriveExcelList, HttpServletResponse response) throws IOException {

        String excelName = "考试答题卷"+ DateUtil.date()+".xlsx";
        //中文名称要进行编码处理
//         这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(excelName, "utf-8");
        response.setHeader("content-type","application/octet-stream");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
        EasyExcel.write(response.getOutputStream(), DeriveExcel.class).sheet("考试答题卷").registerWriteHandler(new CustomCellWriteHandler()).doWrite(deriveExcelList);
    }

    @Override
    public void writeListByIds(String[] ids, HttpServletResponse response) throws IOException {
        try {
            List<DeriveExcel> excelListByExamId = deriveExcelService.selectListByIds(ids);
            for (DeriveExcel datum : excelListByExamId) {
                datum.setPaperImg(new URL(datum.getPaper()));
            }
            writeList(excelListByExamId,response);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeExamByExamId(Integer examId, HttpServletResponse response) throws IOException {
        List<DeriveExcel> excelListByExamId = deriveExcelService.select(examId);

        for (DeriveExcel datum : excelListByExamId) {
            datum.setPaperImg(new URL(datum.getPaper()));
        }

        writeList(excelListByExamId,response);

    }

    @Override
    public String readListByExcel(MultipartFile file) {
        try {
//            ExcelReader excelReader = EasyExcel.read(file.getInputStream(), DeriveExcel.class, new DemoDataListener()).sheet().doRead();
            ExcelReader excelReader = EasyExcel.read(file.getInputStream(), DeriveExcel.class, new DemoDataListener()).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
            // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
            excelReader.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "成功";
    }


}

